package com.whl.sm.controller.notice;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.model.notice.CommunityNotice;
import com.whl.sm.model.notice.UserCpNotice;
import com.whl.sm.model.system.OrgUser;
import com.whl.sm.model.system.OrgUserInfo;
import com.whl.sm.model.system.Role;
import com.whl.sm.service.notice.CommunityNoticeService;
import com.whl.sm.service.notice.UserCpNoticeService;
import com.whl.sm.service.system.OrgUserInfoService;
import com.whl.sm.service.system.OrgUserService;
import com.whl.sm.service.system.RoleService;
import com.whl.sm.vo.UserCpNoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-11-17
 */
@RestController
@RequestMapping("/user-cp-notice")
public class UserCpNoticeController extends BaseController {

    @Autowired
    private UserCpNoticeService userCpNoticeService;

    @Autowired
    private CommunityNoticeService communityNoticeService;

    @Autowired
    private OrgUserInfoService orgUserInfoService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private OrgUserService orgUserService;

    @RequestMapping("/list")
    public Result getUserCpNoticeList(HttpServletRequest request){
        String likeName = request.getParameter("likeName");
        Page<Object> listPage = new Page<>();
        if (likeName!=null && !likeName.equals("undefined") && !likeName.equals("")){
            QueryWrapper recordQueryWrapper = getLikeParam("notice_title");
            List<CommunityNotice> noticeRecord = communityNoticeService.list(recordQueryWrapper);
            if (noticeRecord.size()>0){
                QueryWrapper queryWrapper = new QueryWrapper();
                List<String> noticeIdList = noticeRecord.stream().map(CommunityNotice::getId).collect(Collectors.toList());
                queryWrapper.in("notice_id",noticeIdList);
                setHttpRequestQueryWrapper(queryWrapper,"role_type_id");
                queryWrapper.ne("is_del",1);
                listPage = userCpNoticeService.page(getPage(),queryWrapper);
            }

        }
        else {
            QueryWrapper queryWrapper = new QueryWrapper();
            setHttpRequestQueryWrapper(queryWrapper, "role_type_id");
            queryWrapper.ne("is_del", 1);
            listPage = userCpNoticeService.page(getPage(), queryWrapper);
        }

        List<Object> resultList = listPage.getRecords().stream().map(obj->{
            UserCpNotice item = (UserCpNotice) obj;
            CommunityNotice communityNotice = communityNoticeService.getById(item.getNoticeId());
            List<OrgUserInfo> userInfos =  orgUserInfoService.list(new QueryWrapper<OrgUserInfo>().eq("user_id",item.getUserId()));
            OrgUserInfo orgUserInfo = new OrgUserInfo();
            if (userInfos.size()>0){
                orgUserInfo = userInfos.get(0);
            }else {
                OrgUser orgUser = orgUserService.getById(item.getUserId());
                orgUserInfo.setUserName(orgUser.getLoginName());
            }
            Role role = new Role();
            if (Objects.nonNull(item.getRoleTypeId())){
                role = roleService.getById(item.getRoleTypeId());
            }
            return new UserCpNoticeVo(item,communityNotice,orgUserInfo,role);
        }).collect(Collectors.toList());
        listPage.setRecords(resultList);
        return Result.ok(listPage);
    }

    @RequestMapping("/getByCurrent")
    public Result getByUCurrentUser(){
        String currentUser = orgUserService.getCurrentUserId();
        return userCpNoticeService.getByUserId(currentUser);
    }



    @RequestMapping("/resend")
    public Result logicalDelete(@RequestBody String id){
        userCpNoticeService.update(new UpdateWrapper<UserCpNotice>()
                .eq("id",id)
                .set("read_status",0)
                .set("read_time",null)
        );
        return Result.ok();
    }

    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            userCpNoticeService.update(new UpdateWrapper<UserCpNotice>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }
        return Result.ok();
    }

    @RequestMapping("/hasRead/{id}")
    public Result hasRead(@PathVariable("id") String id){
        return userCpNoticeService.hasReadOperate(id);
    }


}
